﻿var EditorWindow,srcObj,XEditor;
srcObj=window.top.dialogArguments;


if (srcObj.ownerDocument.defaultView)
{
	EditorWindow=srcObj.ownerDocument.defaultView.parent;
	XEditor=EditorWindow.XEditor;
	XEditor.selection=XEditor.getSelection();
}
else
{
	EditorWindow=srcObj.document.parentWindow;

	if (EditorWindow.document.location.toString().indexOf("/editor.")>0)
	{
		if (!(document.location.toString().indexOf("/dialogfind.htm")>0 && srcObj.tagName=="TEXTAREA"))
			EditorWindow=EditorWindow.parent;
	}
	else if (EditorWindow.document.location.toString().indexOf("/dialogs/")>0)
	{
		EditorWindow=srcObj.document.parentWindow.parent.EditorWindow;		
	}
	XEditor=EditorWindow.XEditor;
}

document.getElementById("SkinStyleSheet").href="../"+EditorWindow.WebNoteSkinResourcePath+"editor.css"

function dialogTitle(s)
{
	document.write("<div class='dialogtitle'>"+eval("EditorWindow."+s)+"</div>");
	document.title="WebNoteEditor "+eval("EditorWindow."+s);
}


function dialogPrintlang(s)
{
	document.write(eval("EditorWindow."+s));
}

function PrintButtons()
{
	document.write("<div class=\"divbuttons\">");
	document.write("<input type=\"button\" value=\""+EditorWindow.lang_button_ok+"\" id=\"btnOk\" class=\"sysbtn5\">");
	document.write("<input type=\"button\" value=\""+EditorWindow.lang_button_cancel+"\" id=\"btnCancel\" class=\"sysbtn5\">");
	document.write("<input type=\"button\" value=\""+EditorWindow.lang_button_apply+"\" id=\"btnUse\" class=\"sysbtn5\">");
	document.write("</div>");
	document.getElementById("btnOk").onclick=__btnOk_onclick;
	document.getElementById("btnCancel").onclick=__btnCancel_onclick;
	document.getElementById("btnUse").onclick=__btnUse_onclick;
}

function PrintButtons2()
{
    document.write("<input type=\"button\" value=\""+EditorWindow.lang_button_ok+"\" id=\"btnOk\" class=\"SysBtn5\">");
	document.write("<input type=\"button\" value=\""+EditorWindow.lang_button_cancel+"\" id=\"btnCancel\" class=\"SysBtn5\">");
	document.getElementById("btnOk").onclick=__btnOk_onclick;
	document.getElementById("btnCancel").onclick=__btnCancel_onclick;
}

function PrintButtons3()
{
    document.write("<input type=\"button\" value=\""+EditorWindow.lang_button_ok+"\" id=\"btnOk\" class=\"SysBtn5\">");
	document.getElementById("btnOk").onclick=__btnOk_onclick;
}


function AutoResizeDialogWindow()
{
	__InputRender();
	document.body.scroll="no";
	document.body.style.overflow="hidden";

	if (EditorWindow.$browser.isIE)
		DialogResize();
	else
		centerWindow();
	document.onkeydown=function(event)
	{
		event=(window.event?window.event:event);
		var keycode=(window.event ? window.event.keyCode : event.which);
		switch (keycode)
		{
			case 27:
				window.close();
				break;
			case 13:
				break;
		}
	}
	
}

function centerWindow()  
{
	var xMax=screen.width;
	var yMax=screen.height;
	window.resizeTo(document.body.scrollWidth+16,document.body.scrollHeight+100);
	window.moveTo((xMax-document.body.scrollWidth)/2,(yMax-document.body.scrollHeight)/2-60);
}  


function DialogResize()
{
	//alert("ddd");
 	var posHeight=parseInt(window.dialogHeight)-document.body.clientHeight;
	var posWidth=parseInt(window.dialogWidth)-document.body.clientWidth;
	window.dialogWidth=(document.body.scrollWidth+posWidth)+"px";
	window.dialogHeight=(document.body.scrollHeight+posHeight)+"px";
	//window.dialogHeight=document.body.scrollHeight+20+"px";

	window.dialogLeft=(window.screen.width/2-parseInt(window.dialogWidth)/2)+"px";
	window.dialogTop=(window.screen.height/2-parseInt(window.dialogHeight)/2-60)+"px";
}

//======public functions===========

var xmlDom;

function createXMLHttp() 
{ 
	if(window.XMLHttpRequest) 
	{ 
		return new XMLHttpRequest();
		// 在非Microsoft浏览器中创建XMLHttpRequest对象
	}
	else if(window.ActiveXObject) 
	{ 
		try{ 
			return new ActiveXObject("Microsoft.XMLHTTP");
			//尝试按老版InternetExplorer方法创建
		}
		catch(e1)
		{ 
			try{ 
				return new ActiveXObject("Msxml2.XMLHTTP");
				//尝试按新版InternetExplorer方法创建
			}
			catch(e2) 
			{ 
				alert("XMLHttpRequest object error."); 
			}
		}
	}
}


function createXMLDom(sXMLFileURL,sXML,sOnready)
{
	if (EditorWindow.$browser.isIE)
	{
		xmlDom=new ActiveXObject("Microsoft.XMLDOM");
		xmlDom.async=true;
		if (sOnready)
			xmlDom.onreadystatechange=sOnready;
			
		if (sXMLFileURL)
			xmlDom.load(sXMLFileURL);
		else
			xmlDom.loadXML("<?xml version='1.0' ?><root>" + sXML+ "</root>");
	}
	else
	{	
		sXMLFileURL=sXMLFileURL.replace("\.\./","");
		xmlDom=EditorWindow.__XMLHttpRequest(sXMLFileURL,"GET",null,"xml");
		if (sOnready)
			sOnready();

		return;
	
		/*
		xmlDoc=document.implementation.createDocument('', '', null);
        xmlDoc.load(sXMLFileURL);
        //alert("createXMLDom:"+xmlDoc);
        xmlDoc.onload=sOnready;
		return;

		
		var root;
		xmlDom=document.implementation.createDocument('','',null);
		
		
		xmlDom.parseError  =  {
            valueOf: function(){return this .errorCode; },
            toString:function(){return this .errorCode.toString();}
        };
        		
		if (sOnready)
			xmlDom.addEventListener("load",sOnready,false);

		if (sXMLFileURL)
			xmlDom.load(sXMLFileURL);
		else
    		xmlDom=(new DOMParser()).parseFromString("<?xml version='1.0' ?><root>" + sXML+ "</root>","text/xml");
    	*/
    		
    }
}

//check input value number
function __InputRender()
{
	var i,elems;
	elems=document.getElementsByTagName("input");
	for (i=0;i<elems.length;i++)
	{
		if (elems[i].getAttribute("datatype")=="number")
		{
			elems[i].onkeyup=__Input_onkeyup;
		}
	}
	elems=null;
}

function __Input_onkeyup()
{
	this.value=parseInt(this.value);
	if (isNaN(this.value))
		this.value="";
}

//element background setting
function __boundElementBgImage()
{
	document.getElementById("backgroundImage").innerText=srcObj.style.backgroundImage.toString().replace("url(","").replace(")","");
	document.getElementById("InputbackgroundImage").onchange=function()
	{
		document.getElementById("backgroundImage").innerText=document.getElementById("InputbackgroundImage").value;
	};
	document.getElementById("btnRemovebackgroundImage").onclick=function()
	{
		document.getElementById("backgroundImage").innerText="";
	};
}

function __setBgSetting()
{
	__boundElementBgImage();
	document.getElementById("InputBgColor").value=srcObj.style.backgroundColor?srcObj.style.backgroundColor:srcObj.bgColor;
	if (document.getElementById("InputBgColor").value=="undefined")
		document.getElementById("InputBgColor").value="";
	EditorWindow._FormfocusSelect("selectbackgroundPositionY",srcObj.style.backgroundPositionY,document);
	EditorWindow._FormfocusSelect("selectbackgroundPositionX",srcObj.style.backgroundPositionX,document);
	EditorWindow._FormfocusSelect("selectbackgroundRepeat",srcObj.style.backgroundRepeat,document);
}

function __applyBgSetting()
{
	srcObj.style.backgroundColor=document.getElementById("InputBgColor").value;

	if (document.getElementById("backgroundImage").innerText!="")
		srcObj.style.backgroundImage="url('"+document.getElementById("backgroundImage").innerText+"')";
	else
		srcObj.style.backgroundImage="";
	
	if (document.getElementById("InputBgColor").value!="")
		srcObj.style.backgroundColor=document.getElementById("InputBgColor").value;
	if (document.getElementById("backgroundImage").innerText!="" || document.getElementById("InputBgColor").value!="")
	{	
		srcObj.style.backgroundPositionY=document.getElementById("selectbackgroundPositionY").value;
		srcObj.style.backgroundPositionX=document.getElementById("selectbackgroundPositionX").value;
		srcObj.style.backgroundRepeat=document.getElementById("selectbackgroundRepeat").value;
	}
	else
	{
		srcObj.style.backgroundPositionY="";
		srcObj.style.backgroundPositionX="";
		srcObj.style.backgroundRepeat="";
	}

}

function __setWidthAndHeight()
{
	var Value;
	if (srcObj.style.width)
	{
		document.getElementById("Cellwidth").checked=true;
		__Cellwidth_onclick();
		Value=srcObj.style.width;
		document.getElementById("InputWidth").value=parseInt(Value);
		EditorWindow._FormfocusRadio("CellWidthpx",Value.replace(parseInt(Value),""),document);
	}
	if (srcObj.style.height)
	{
		document.getElementById("Cellheight").checked=true;
		__Cellheight_onclick();
		Value=srcObj.style.height;
		document.getElementById("InputHeight").value=parseInt(Value);
		EditorWindow._FormfocusRadio("CellHeightpx",Value.replace(parseInt(Value),""),document);
	}
	
	document.getElementById("Cellwidth").onclick=__Cellwidth_onclick;
	document.getElementById("Cellheight").onclick=__Cellheight_onclick;
}

function __applyWidthAndHeight()
{
	if (document.getElementById("Cellwidth").checked && document.getElementById("InputWidth").value!="")
	{
		srcObj.style.width=document.getElementById("InputWidth").value+EditorWindow._FormgetRadioValue("CellWidthpx",document);
	}

	if (document.getElementById("Cellheight").checked && document.getElementById("InputHeight").value!="")
	{
		srcObj.style.height=document.getElementById("InputHeight").value+EditorWindow._FormgetRadioValue("CellHeightpx",document);
	}

}

function __Cellwidth_onclick()
{
	document.getElementById("InputWidth").disabled=document.getElementsByName("CellWidthpx")[0].disabled=document.getElementsByName("CellWidthpx")[1].disabled=!document.getElementById("Cellwidth").checked;
}

function __Cellheight_onclick()
{
	document.getElementById("InputHeight").disabled=document.getElementsByName("CellHeightpx")[0].disabled=document.getElementsByName("CellHeightpx")[1].disabled=!document.getElementById("Cellheight").checked;
}


//select color
function selectColorIntoInput(sInputId,srcElement)
{
	var returnValue;
	returnValue=EditorWindow._showWindialog("dialogs/dialogcolor.htm?color="+document.getElementById(sInputId).value,XEditor.document.body,100,300,window);
	
	if (returnValue!=null)
	{
		document.getElementById(sInputId).value=returnValue;
		srcElement.style.backgroundColor=returnValue;
	}
}

function selectTarget(sInputId,srcElement)
{
	//window.open("dialogtarget.htm")
	var returnValue;
	returnValue=EditorWindow._showWindialog("dialogs/dialogtarget.htm",XEditor.document.body,80,80,window);
	
	if (returnValue!=null)
	{
		document.getElementById(sInputId).value=returnValue;
	}
}

